
/**
 * @file rtc.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       外挂时钟
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */


#include "rtc.h"

#include "stm32f10x.h"                  // Device header

#include <time.h>

#include <string.h>

#include "LCD.h"

#include "LCD_init.h"


uint16_t RTCa_TIME[7]={2024,1,29,19,44,25,1};//前面补零的数字是八进制，实现不了




//time_t time_cnt;//实际上是int64类型的数
//struct tm  time_data; //结构体类型名time_data
//char *time_str;//指向时间的字符串指针
//char times[50];
////time_cnt=time(NULL);//获取时间戳秒数//time(&time_cnt)
//time_cnt=1672588795;//手动给时间

//time_data=*gmtime(&time_cnt);//time_data=*localtime(&time_cnt);//本地时间自动添加时区偏移

//结构体成员
//time_data.tm_year+1900;年
//time_data.tm_mon+1;  		月
//time_data.tm_mday; 		日
//time_data.tm_hour; 		时
//time_data.tm_min ; 		分
//time_data.tm_sec;   		秒
//time_data.tm_wday ；		星期

//time_str=cti                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        me(&time_cnt);//字符串转换



//BPK//
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
//PWR_BackupAccessCmd(ENABLE);
//BKP_WriteBackupRegister(BKP_DR1,0x234);//中容量1~10大容量1~42
//BKP_ReadBackupRegister(BKP_DR1)




void RTCa_init(void)
{
	
	
	
	//PC13~PC15面包板寄生电容会干扰RTC时钟
	
	
	
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
	PWR_BackupAccessCmd(ENABLE);//开启时钟，使能BKP和RTC访问
	
	//开启LSE时钟，等待启动完成
	
	if(BKP_ReadBackupRegister(BKP_DR1)!=0xA5A5)//如果备用寄存器重置为0，则进行初始化
	{
		RCC_LSEConfig(RCC_LSE_ON);//启动晶振//可以解旁路外部时钟
		
		while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)!=SET);//等待LSE是否启动
		
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//选择LSE时钟源
		
		RCC_RTCCLKCmd(ENABLE );//使能时钟
		
		RTC_WaitForSynchro();//等待同步	
		RTC_WaitForLastTask();//等待上一次操作完成
		
		RTC_SetPrescaler(32768-1);//LSE频率32.768khz//自带进入和退出配置模式的代码	
		RTC_WaitForLastTask();//用等待函数将写入操作隔开
		
	//	RTC_SetCounter(1672588795);//设定初始时间//CNT的值以此刻开始一秒自增
	//	RTC_WaitForLastTask();
		RTCa_time();
		
		BKP_WriteBackupRegister(BKP_DR1,0xA5A5);
	}
	else
	{
		RTC_WaitForSynchro();//等待同步	
		RTC_WaitForLastTask();//等待上一次操作完成
	}
	
}

void RTCa_time(void)  //写入时间
{
	time_t time_cnt ;
	struct tm time_data;
	
	time_data.tm_year=RTCa_TIME[0]-1900;//减去偏移
	time_data.tm_mon=RTCa_TIME[1]-1;  	
	time_data.tm_mday=RTCa_TIME[2]; 	
	time_data.tm_hour=RTCa_TIME[3]; 	
	time_data.tm_min=RTCa_TIME[4] ; 	
	time_data.tm_sec=RTCa_TIME[5];   	
	time_data.tm_wday =RTCa_TIME[6];	
	
	time_cnt=mktime(&time_data); //- 8 * 60 * 60;//由日期到秒数
	
	RTC_SetCounter(time_cnt);//指定的
	RTC_WaitForLastTask();
}


void RTCa_readtime(void )//读取时间
{
	time_t time_cnt ;
	struct tm time_data;
	
	time_cnt=RTC_GetCounter();//+8*60*60;//转化成东八区
	
	time_data= *localtime (&time_cnt);
	
	
	RTCa_TIME[0]=time_data.tm_year+1900;//减去偏移
	RTCa_TIME[1]=time_data.tm_mon +1;  	
	RTCa_TIME[2]=time_data.tm_mday; 	
	RTCa_TIME[3]=time_data.tm_hour; 	
	RTCa_TIME[4]=time_data.tm_min  ; 	
	RTCa_TIME[5]=time_data.tm_sec ;  
	RTCa_TIME[6]=time_data.tm_wday ;	
}


void BeiJing_time(void)
{
	RTCa_readtime();
	
	
	
	LCD_ShowIntNum(24,1,RTCa_TIME[0],4,BLUE ,LIGHTBLUE ,12);
	LCD_ShowChinese(48,1,"年",LIGHTBLUE,LIGHTBLUE,12,1);//
	LCD_ShowIntNum(60,1,RTCa_TIME[1],2,BLUE ,LIGHTBLUE ,12);
	LCD_ShowChinese(72,1,"月",LIGHTBLUE,WHITE,12,1);
	
	
	LCD_ShowIntNum(84,1,RTCa_TIME[2],2,BLUE ,LIGHTBLUE ,12);
	LCD_ShowChinese(96,1,"日",LIGHTBLUE,WHITE,12,1);
	
	
	LCD_ShowIntNum(42,100,RTCa_TIME[3],2,BLUE ,LIGHTBLUE ,12);
	
	LCD_ShowString(54,100,":",BLUE,LIGHTBLUE,12,0);
	
	LCD_ShowIntNum(60,100,RTCa_TIME[4],2,BLUE ,LIGHTBLUE ,12);
	LCD_ShowString(72,100,":",BLUE,LIGHTBLUE,12,0);
	
	
	LCD_ShowIntNum(78,100,RTCa_TIME[5],2,BLUE ,LIGHTBLUE ,12);
	
	
	
	LCD_ShowChinese(48,14,"星期",BLUE,LIGHTBLUE ,12,1);
	
	
	switch(RTCa_TIME[6])
	{
		case 1:LCD_ShowChinese(72,14,"一",BLUE,LIGHTBLUE ,12,0);break;
		case 2:LCD_ShowChinese(72,14,"二",BLUE,LIGHTBLUE ,12,0);break;
		case 3:LCD_ShowChinese(72,14,"三",BLUE,LIGHTBLUE ,12,0);break;
		case 4:LCD_ShowChinese(72,14,"四",BLUE,LIGHTBLUE ,12,0);break;
		case 5:LCD_ShowChinese(72,14,"五",BLUE,LIGHTBLUE ,12,0);break;
		case 6:LCD_ShowChinese(72,14,"六",BLUE,LIGHTBLUE ,12,0);break;
		case 7:LCD_ShowChinese(72,14,"日",BLUE,LIGHTBLUE ,12,0);break;
	}
	

	
	
}


void beijing_time0(void)
{
	
	
	
	LCD_ShowIntNum(10,54,RTCa_TIME[0],4,BLUE ,WHITE ,12);
	LCD_ShowIntNum(34,54,RTCa_TIME[1],2,BLUE ,WHITE ,12);	
	LCD_ShowIntNum(48,54,RTCa_TIME[2],2,BLUE ,WHITE ,12);	
	LCD_ShowIntNum(70,54,RTCa_TIME[3],2,BLUE ,WHITE ,12);
	LCD_ShowString(82,54,":",DARKBLUE,WHITE,12,0);	
	LCD_ShowIntNum(88,54,RTCa_TIME[4],2,BLUE ,WHITE ,12);
	LCD_ShowString(100,54,":",DARKBLUE,WHITE,12,0);
	LCD_ShowIntNum(106,54,RTCa_TIME[5],2,BLUE ,WHITE ,12);
	
}

void beijing_time1(void)
{
	
	LCD_ShowIntNum(10,5,RTCa_TIME[0],4,BLUE ,WHITE ,12);
	LCD_ShowIntNum(34,5,RTCa_TIME[1],2,BLUE ,WHITE ,12);
	LCD_ShowIntNum(48,5,RTCa_TIME[2],2,BLUE ,WHITE ,12);
	LCD_ShowIntNum(70,5,RTCa_TIME[3],2,BLUE ,WHITE ,12);
	LCD_ShowString(82,5,":",DARKBLUE,WHITE,12,0);	
	LCD_ShowIntNum(88,5,RTCa_TIME[4],2,BLUE ,WHITE ,12);
	LCD_ShowString(100,5,":",DARKBLUE,WHITE,12,0);
	LCD_ShowIntNum(106,5,RTCa_TIME[5],2,BLUE ,WHITE ,12);
	
}




